package com.avic.map.controller;

import com.avic.map.common.util.lang.ServiceCallBack;
import com.avic.map.controller.info.TreeOfMapAudioChildrenInfo;
import com.avic.map.controller.info.TreeOfMapFirstInAudioInfo;
import com.avic.map.controller.info.TreeOfMapVideoChildrenInfo;
import com.avic.map.controller.info.TreeOfMapVideoFirstInfo;
import com.avic.map.controller.result.TreeOfMapAudioRes;
import com.avic.map.controller.result.TreeOfMapVideoRes;
import com.avic.map.dal.model.CarResource;
import com.avic.map.dal.model.SingleSoldierResource;
import com.avic.map.dal.model.UavResource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author huangsheng on 2018/12/23
 */

@Slf4j
@RestController
@RequestMapping("tree")
@Api(tags = "数据树接口", description = "视频树数据，语音数据数据")
public class TreeController extends AbstractFacadeImpl {

    @GetMapping("video")
    @ApiOperation(value = "视频树数据")
    public TreeOfMapVideoRes treeOfMapVideo() {
        log.info("视频树数据-treeOfMapVideo");
        final TreeOfMapVideoRes result = new TreeOfMapVideoRes();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
            }

            @Override
            public void doService() throws Exception {

                // 处理单兵数据
                List<SingleSoldierResource> singleSoldierResources = singleSoldierResourceService.querySoldierTreeRecords(SingleSoldierResource.EQUIPMENT_TYPE_4G);
                List<TreeOfMapVideoFirstInfo> treeOfMapVideoFirstInfos = new ArrayList<>();

                if (null != singleSoldierResources && singleSoldierResources.size() > 0) {
                    TreeOfMapVideoFirstInfo treeOfMapVideoFirstInfo = new TreeOfMapVideoFirstInfo();
                    treeOfMapVideoFirstInfo.setLabel(TreeOfMapVideoFirstInfo.LABEL_SOLIDER);
                    List<TreeOfMapVideoChildrenInfo> treeOfMapVideoChildrenInfos = new ArrayList<>();
                    singleSoldierResources.forEach(singleSoldierResource -> {
                        TreeOfMapVideoChildrenInfo treeOfMapVideoChildrenInfo = new TreeOfMapVideoChildrenInfo();
                        treeOfMapVideoChildrenInfo.setId(singleSoldierResource.getPersonnelNumber());
                        treeOfMapVideoChildrenInfo.setLabel(singleSoldierResource.getPersonnelNumber() + "（" + singleSoldierResource.getPersonnelCompany() + "）");
                        treeOfMapVideoChildrenInfo.setRemark(StringUtils.EMPTY);
                        treeOfMapVideoChildrenInfos.add(treeOfMapVideoChildrenInfo);
                    });
                    treeOfMapVideoFirstInfo.setChildren(treeOfMapVideoChildrenInfos);
                    treeOfMapVideoFirstInfos.add(treeOfMapVideoFirstInfo);
                }

                List<UavResource> uavResources = uavResourceService.queryUavTreeRecords();
                if (null != uavResources && uavResources.size() > 0) {
                    TreeOfMapVideoFirstInfo treeOfMapVideoFirstInfo = new TreeOfMapVideoFirstInfo();
                    treeOfMapVideoFirstInfo.setLabel(TreeOfMapVideoFirstInfo.LABEL_UAV);
                    List<TreeOfMapVideoChildrenInfo> treeOfMapVideoChildrenInfos = new ArrayList<>();
                    uavResources.forEach(uavResource -> {
                        TreeOfMapVideoChildrenInfo treeOfMapVideoChildrenInfo = new TreeOfMapVideoChildrenInfo();
                        treeOfMapVideoChildrenInfo.setId(uavResource.getUavNumber());
                        treeOfMapVideoChildrenInfo.setLabel(uavResource.getUavNumber() + "（" + uavResource.getUavName() + "）");
                        treeOfMapVideoChildrenInfo.setRemark(uavResource.getUavRtsp());
                        treeOfMapVideoChildrenInfos.add(treeOfMapVideoChildrenInfo);
                    });
                    treeOfMapVideoFirstInfo.setChildren(treeOfMapVideoChildrenInfos);
                    treeOfMapVideoFirstInfos.add(treeOfMapVideoFirstInfo);
                }
                result.setTreeOfMapVideoFirstInfos(treeOfMapVideoFirstInfos);
                result.setSuccess(true);
            }
        });
    }


    @GetMapping("audio")
    @ApiOperation(value = "通话语音树数据")
    public TreeOfMapAudioRes treeOfMapAudioRes() {
        log.info("视频树数据-treeOfMapVideo");
        final TreeOfMapAudioRes result = new TreeOfMapAudioRes();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
            }

            @Override
            public void doService() throws Exception {

                // 处理单兵数据
                List<SingleSoldierResource> singleSoldierResources = singleSoldierResourceService.querySoldierTreeRecords(SingleSoldierResource.EQUIPMENT_TYPE_AUDIO);
                List<TreeOfMapFirstInAudioInfo> treeOfMapFirstInAudioInfos = new ArrayList<>();

                if (null != singleSoldierResources && singleSoldierResources.size() > 0) {
                    TreeOfMapFirstInAudioInfo treeOfMapFirstInAudioInfo = new TreeOfMapFirstInAudioInfo();
                    treeOfMapFirstInAudioInfo.setLabel(TreeOfMapFirstInAudioInfo.LABEL_SOLIDER);
                    List<TreeOfMapAudioChildrenInfo> treeOfMapAudioChildrenInfos = new ArrayList<>();
                    singleSoldierResources.forEach(singleSoldierResource -> {
                        TreeOfMapAudioChildrenInfo treeOfMapAudioChildrenInfo = new TreeOfMapAudioChildrenInfo();
                        treeOfMapAudioChildrenInfo.setId(singleSoldierResource.getPersonnelNumber());
                        treeOfMapAudioChildrenInfo.setLabel(singleSoldierResource.getPersonnelNumber() + "（" + singleSoldierResource.getPersonnelCompany() + "）");
                        treeOfMapAudioChildrenInfos.add(treeOfMapAudioChildrenInfo);
                    });
                    treeOfMapFirstInAudioInfo.setChildren(treeOfMapAudioChildrenInfos);
                    treeOfMapFirstInAudioInfos.add(treeOfMapFirstInAudioInfo);
                }

                List<CarResource> carResources = carResourceService.queryCarTreeRecords();
                if (null != carResources && carResources.size() > 0) {
                    TreeOfMapFirstInAudioInfo treeOfMapFirstInAudioInfo = new TreeOfMapFirstInAudioInfo();
                    treeOfMapFirstInAudioInfo.setLabel(TreeOfMapFirstInAudioInfo.LABEL_CAR);
                    List<TreeOfMapAudioChildrenInfo> treeOfMapAudioChildrenInfos = new ArrayList<>();
                    carResources.forEach(carResource -> {
                        TreeOfMapAudioChildrenInfo treeOfMapAudioChildrenInfo = new TreeOfMapAudioChildrenInfo();
                        treeOfMapAudioChildrenInfo.setId(carResource.getCarNumber());
                        treeOfMapAudioChildrenInfo.setLabel(carResource.getCarNumber() + "（" + carResource.getCarName() + "）");
                        treeOfMapAudioChildrenInfos.add(treeOfMapAudioChildrenInfo);
                    });
                    treeOfMapFirstInAudioInfo.setChildren(treeOfMapAudioChildrenInfos);
                    treeOfMapFirstInAudioInfos.add(treeOfMapFirstInAudioInfo);
                }
                result.setTreeOfMapFirstInAudioInfos(treeOfMapFirstInAudioInfos);
                result.setSuccess(true);
            }
        });
    }
}
